package com.meshnetics.gb.stack.mac.primitives;

import com.meshnetics.gb.stack.core.event.StackEventType;
import com.meshnetics.gb.stack.core.event.StackRequest;
import com.meshnetics.gb.stack.mac.RadioMACStackLayer;
import com.meshnetics.gb.stack.mac.type.GTSCharacteristics;
import com.meshnetics.gb.type.impl.ByteUtils;
import com.meshnetics.gb.type.impl.zcl.Bool;
import com.meshnetics.gb.type.impl.zcl.TypeBool;

import java.nio.ByteBuffer;

/**
 * WRITE DESCRIPTION HERE
 *
 * @author AKovylin
 */
public class MACGtsRequest extends StackRequest implements MACCommand, MACReplyAwaited {
    public static final byte CODE = 0x4f;

    private final GTSCharacteristics gtsCharacts;
    private final Bool securityEnable;

    public MACGtsRequest(GTSCharacteristics gtsCharacts, boolean securityEnable) {
        this(gtsCharacts, Boolean.valueOf(securityEnable));
    }

    public MACGtsRequest(GTSCharacteristics gtsCharacts, Boolean securityEnable) {
        this(gtsCharacts, (Bool) TypeBool.BOOLEAN.fromObject(securityEnable));
    }

    public MACGtsRequest(GTSCharacteristics gtsCharacts, Bool securityEnable) {
        super("macGtsRequest", StackEventType.LME_TYPE);
        if (gtsCharacts == null) {
            throw new NullPointerException("gtsCharacts is null");
        }
        if (securityEnable == null) {
            throw new NullPointerException("securityEnable is null");
        }
        if (securityEnable.isInvalid()) {
            throw new IllegalArgumentException("invalid securityEnable");
        }
        this.gtsCharacts = gtsCharacts;
        this.securityEnable = securityEnable;
    }

    public GTSCharacteristics getGtsCharacts() {
        return gtsCharacts;
    }

    public Bool getSecurityEnableType() {
        return securityEnable;
    }

    public Boolean getSecurityEnable() {
        return (Boolean) securityEnable.toObject();
    }

    public byte getCode() {
        return CODE;
    }

    public int getSize(RadioMACStackLayer layer) {
        return 2;
    }

    public void serialize(ByteBuffer bb, RadioMACStackLayer layer) {
        ByteUtils.ensureNotNull(bb);
        gtsCharacts.serialize(bb);
        securityEnable.serialize(bb);
    }

    public String toString() {
        return getName() + '(' + gtsCharacts + ',' + securityEnable + ')';
    }
}
